package com.jumeng.controller;

import com.jumeng.server.AuthLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("/api/check")
@Api(tags = "认证接口")
public class AuthLoginController {

    /**
     * 若确认此次GET请求来自微信服务器，请原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败。加密/校验流程如下：
     */

    @Autowired
    private AuthLoginService authLoginService;

    @GetMapping("/check-token")
    @ApiOperation(value = "验证消息的确来自微信服务器")
    public String checkToken(@RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp,
                             @RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr) {
        return authLoginService.checkSignature(signature, timestamp, nonce, echostr);
    }

    /**
     * 用户同意授权，获取用户信息,作为页面跳转使用
     */
    @GetMapping("/oauth2/authorize")
    @ApiOperation(value = "用户同意授权，获取用户信息")
    public void oauth2Authorize() {
        authLoginService.oauth2Authorize();
    }

    /**
     * 用户关注后获取用户的openId并保存
     *
     * @param code
     * @return
     */
    @GetMapping("/receiveOpenId")
    @ApiOperation(value = "用户关注后获取用户的openId并保存")
    public String receiveOpenId(@RequestParam("code") String code) {
        return authLoginService.receiveOpenId(code);
    }

}
